package com.test.service;

import com.test.dao.CustomerDao;
import com.test.dao.DishDao;
import com.test.dao.MerchantDao;
import com.test.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CustomerService {

    private final CustomerDao customerDao;

    private final MerchantDao merchantDao;

    private final DishDao dishDao;

    @Autowired
    public CustomerService(CustomerDao customerDao, MerchantDao merchantDao, DishDao dishDao) {
        this.customerDao = customerDao;
        this.merchantDao = merchantDao;
        this.dishDao = dishDao;
    }





    public Customer customerInfo(String phoneNumber){
        return customerDao.getCustomerByPhoneNumber(phoneNumber);
    }

    public int registerAndLogin(User user){
        return customerDao.registerAndLogin(user.getType(), user.getUsername(), user.getPassword());
    }

    public List<Shop> getShopListByKeyword(String keyword){
        List<Shop> shopList = new ArrayList<>();

        List<Merchant> merchantListLike = merchantDao.getMerchantListByKeyword(keyword);
        for (Merchant merchant : merchantListLike) {
            List<Dish> allDishList = dishDao.getDishListByPhoneNumber(merchant.getMerchantPhoneNumber());
            shopList.add(new Shop(merchant,allDishList));
        }


        //店名当中没有匹配的
        List<Merchant> merchantListNotLike = merchantDao.getMerchantListByKeywordNotLike(keyword);
        for (Merchant merchant : merchantListNotLike) {
            //初步筛选，若列表不为空仍返回该商家的所有菜品信息，而不是只有搜索到的菜品信息

            List<Dish> dishList=dishDao.getDishListByMerchantPhoneNumberAndKeyword(merchant.getMerchantPhoneNumber(),keyword);

            if(!dishList.isEmpty()){
                List<Dish> allDishList = dishDao.getDishListByPhoneNumber(merchant.getMerchantPhoneNumber());
                shopList.add(new Shop(merchant,allDishList));
            }
        }


        return shopList;
    }

    public List<Shop> getShopList(){
        List<Shop> shopList = new ArrayList<>();

        List<String> merchantNumberList = dishDao.getListByRecommend();

        for (String merchantNumber : merchantNumberList) {
            Merchant merchant = merchantDao.getMerchantByPhoneNumber(merchantNumber);
            List<Dish> allDishList = dishDao.getDishListByPhoneNumber(merchantNumber);
            shopList.add(new Shop(merchant,allDishList));
        }




        return shopList;
    }

    public void updateMember(){
        List<String> list = customerDao.getUserName();
        for (String s : list) {
            customerDao.DetermineMembershipLevel(s);
        }
    }

    public List<Customer> getAllCustomerList(){
        return customerDao.getCustomerList();
    }

    public void forbidden(String employeeNumber,String customerPhoneNumber){
        int judge = customerDao.judge(customerPhoneNumber);
        System.out.println(judge);
        if (judge==0){//有问题的账户
            long time = System.currentTimeMillis();
            java.sql.Date date = new java.sql.Date(time);
            customerDao.updateForbiddenTimeByPhoneNumber(date,employeeNumber,customerPhoneNumber);
        }
    }

}
